﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Bon.It.Proxy;
using Bon_It.Model;
using Bonit_It.ViewModel.Base;

namespace Bonit_It.ViewModel.MasterData
{
    public class AllArticleViewModel: ListViewModel
    {
        /// <summary>
        /// The article from the database.
        /// </summary>
        public ObservableCollection<Artikel> Source { get; set; }

       
        
        /// <summary>
        /// The current selected item.
        /// </summary>
        public Artikel SelectedItem { get; set; }

     
        public AllArticleViewModel()
        {
            ShowDetails  = new RelayCommand(OnShowDetails, CanShowDetails);
            
            RefreshLookups();
            
            DisplayName = string.Format("Artikelübersicht ({0} Datensätze)", Source.Count);
        }

        #region Overrides of ViewModel

        #region commands

         

        public bool CanShowDetails(object arg)
        {
            return true;
        }

        public void OnShowDetails(object args)
        {
            ArticleDetailsViewModel model = new ArticleDetailsViewModel(SelectedItem);
            OnAttachWorkspace(model);
        }
        
        #endregion
        
        public override void RefreshLookups()
        {
            
            Source = new ObservableCollection<Artikel>(ServiceProxy.Instance.GetAllArticles());
            SourceView = CollectionViewSource.GetDefaultView(Source);
            SourceView.Filter = Filter;
        }

        /// <summary>
        /// Filters the specified elements.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <returns></returns>
        public override bool Filter (object o)
        {
            // no filter there.
            if (string.IsNullOrEmpty(FilterText))
            {
                return true;
            }

            Artikel item = o as Artikel;
            int i;
            if (int.TryParse(FilterText, out i))
            {
                if (item.EANNummer.Equals(i))
                {
                    return true;
                }
            }
            else
            {
                if (item.Bezeichnung.StartsWith(FilterText, StringComparison.InvariantCultureIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }

        public override void CleanUp()
        {
            Source.Clear();
            Source = null;
        }

        public override bool CanDelete(object argument)
        {
            return Source.Count>0 && SelectedItem!= null;
        }

        public override bool CanSave(object argument)
        {
            return false;
        }


        public override bool CanCreate(object argument)
        {
            // check if the requirements is met. 
            // this can be a bottleneck ... but we don't expect hughe data here.
            var comoditygroups = ServiceProxy.Instance.GetComodityGroups();
            var taxes = ServiceProxy.Instance.GetTaxes();
            var gebinde= ServiceProxy.Instance.GetAllGebinde();
            return comoditygroups.Count() > 0 && taxes.Count() > 0 && gebinde.Count() > 0;
        }

        public override void OnDelete(object argument)
        {
            if (BeforeDelete() == MessageBoxResult.Yes)
            {
                Debug.Assert(SelectedItem != null, "The selected item is null!");
                ServiceProxy.Instance.DeleteArticle(SelectedItem);
                Source.Remove(SelectedItem);
            }
        }

        public override void OnCreate(object argument)
        {
            ArticleDetailsViewModel model = new ArticleDetailsViewModel();
            model.SelectedItem = new Artikel();
            OnAttachWorkspace(model);
        }
        #endregion
    }
}
